package com.ruoyi.shop.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.shop.util.ImageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.domain.ShopAd;
import com.ruoyi.shop.service.IShopAdService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 广告Controller
 * 
 * @author Alan Zhang
 * @date 2021-09-12
 */
@Controller
@RequestMapping("/shop/ad")
public class ShopAdController extends BaseController
{
    private String prefix = "shop/ad";

    @Autowired
    private IShopAdService shopAdService;

    @RequiresPermissions("shop:ad:view")
    @GetMapping()
    public String ad()
    {
        return prefix + "/ad";
    }

    /**
     * 查询广告列表
     */
    @RequiresPermissions("shop:ad:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ShopAd shopAd)
    {
        startPage();
        List<ShopAd> list = shopAdService.selectShopAdList(shopAd);
        return getDataTable(list);
    }

    /**
     * 导出广告列表
     */
    @RequiresPermissions("shop:ad:export")
    @Log(title = "广告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ShopAd shopAd)
    {
        List<ShopAd> list = shopAdService.selectShopAdList(shopAd);
        ExcelUtil<ShopAd> util = new ExcelUtil<ShopAd>(ShopAd.class);
        return util.exportExcel(list, "广告数据");
    }

    /**
     * 新增广告
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存广告
     */
    @RequiresPermissions("shop:ad:add")
    @Log(title = "广告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam(value = "file", required = false) MultipartFile file, ShopAd shopAd) throws IOException {
        if(file != null) {
            shopAd.setImageUrl(ImageUtils.uploadAd(file));
        }
        return toAjax(shopAdService.insertShopAd(shopAd));
    }

    /**
     * 修改广告
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ShopAd shopAd = shopAdService.selectShopAdById(id);
        mmap.put("shopAd", shopAd);
        return prefix + "/edit";
    }

    /**
     * 修改保存广告
     */
    @RequiresPermissions("shop:ad:edit")
    @Log(title = "广告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam(value = "file", required = false) MultipartFile file, ShopAd shopAd) throws IOException {
        if(file != null) {
            shopAd.setImageUrl(ImageUtils.uploadAd(file));
        }
        return toAjax(shopAdService.updateShopAd(shopAd));
    }

    /**
     * 删除广告
     */
    @RequiresPermissions("shop:ad:remove")
    @Log(title = "广告", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(shopAdService.deleteShopAdByIds(ids));
    }
}
